package com.example.christina.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.christina.enity.Customer;
import com.example.christina.enity.Administrator;

import com.example.christina.service.AdminService;
import com.example.christina.service.CustomService;

@RestController
@RequestMapping(value = "login")
public class LoginController {
    @Autowired
    AdminService adminService;
    @Autowired
    CustomService customService;
    @GetMapping(path = "customer")
    public String customer(@RequestParam(required = false) String name,@RequestParam(required = false) String psw)
    {
        String res = "";
        Customer customer = customService.getCustomer(name);
        String realName = customer.getUsername();
        String realPsw = customer.getPassword();
        if(realName.equals(name))
        {
            if (realPsw.equals(psw))
                res = "登录成功";
            else  if(!realPsw.equals(psw))
                res = "用户密码错误";
        }
        else if(!realName.equals(name))
        {
            res = "用户名错误";
        }
        System.out.println(name + " " + realName + " " + psw + " " + realPsw);
        return res;
    }

    @GetMapping(path = "admin")
    public String admin(@RequestParam(required = false) String name,@RequestParam(required = false) String psw)
    {
        String res = "";
        Administrator administrator = adminService.getOne(name);
        String realName = administrator.getAdminname();
        String realPsw = administrator.getAdminpassword();
        if(realName==name)
        {
            if (realPsw == psw)
                res = "登录成功";
            else  if(realPsw != psw)
                res = "用户密码错误";
        }
        else if(realName != name)
        {
            res = "用户名错误";
        }
        return res;
    }
}
